﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Xml.Linq;
using WeChat.Common;
using WeChat.Model;
using WeChat.View;
using System.Linq;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace WeChat.ViewModel
{
    class ChatContentViewModel : NotifyBase
    {
        public ObservableCollection<ChatMessageModel> chatMessage { get; set; } = new ObservableCollection<ChatMessageModel>();

        public CommonBase OpenChatChouTiCommand { get; set; }

        private bool isOpenPopup;

        public bool IsOpenPopup
        {
            get { return isOpenPopup; }
            set { isOpenPopup = value; this.DoNotify(); }
        }

        public ChatContentViewModel()
        {
            this.isOpenPopup = false;
            this.getChatMessage();
            this.OpenChatChouTiCommand = new CommonBase();
            this.OpenChatChouTiCommand.DoExecute = new Action<object>(DoOpenChatChouTi);
            this.OpenChatChouTiCommand.DoCanExecute = new Func<object, bool>((o) => { return true; });
        }

        private void DoOpenChatChouTi(object obj)
        {
            object[] multiObj = obj as object[];
            Window myWindow = multiObj[0] as Window;
            Border myBorder = multiObj[1] as Border;
            Popup myPopup = multiObj[2] as Popup;
            Button myButton = multiObj[3] as Button;

            //bool myPopupIsOpen = myPopup.IsOpen;
            bool myPopupIsOpen = this.isOpenPopup;
            if (myPopupIsOpen)
            {
                myPopup.IsOpen = false;
                this.isOpenPopup = false;
                myPopup.FlowDirection = FlowDirection.RightToLeft;
                myPopup.PopupAnimation = PopupAnimation.Fade;
            }
            else
            {
                myPopup.IsOpen = true;
                this.isOpenPopup = true;
                Point mousePoint = Mouse.GetPosition(myButton);
                if (myWindow.WindowState == WindowState.Normal)
                {
                    myPopup.HorizontalOffset = mousePoint.X + 34;
                    myPopup.VerticalOffset = mousePoint.Y - 41;
                    myPopup.FlowDirection = FlowDirection.LeftToRight;
                    myPopup.PopupAnimation = PopupAnimation.Fade;
                    myBorder.Height = Application.Current.MainWindow.Height;
                }
                else
                {
                    myPopup.HorizontalOffset = mousePoint.X;
                    myPopup.VerticalOffset = mousePoint.Y + 25;
                    myPopup.FlowDirection = FlowDirection.LeftToRight;
                    myPopup.PopupAnimation = PopupAnimation.Fade;
                    myBorder.Height = Application.Current.MainWindow.Height - 68;
                }
            }
        }

        private void Dialog_Closed(object? sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void getChatMessage()
        {
            this.chatMessage.Add(new ChatMessageModel { Message = "你好", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "好久不见,老铁", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "来杯二锅头", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "gogogo", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "0000000000000000000000000000000000000000000000000000000000000", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "你才能大慈大悲能促进大脑常见的从你的CDC的就曾经的承诺的基础才能打击报复经典时刻都能参加当年吃vhi渐渐少女九点半v哈佛博士查看dock的你曾经你多久才能到家CEO热哦日俄哦i肉开发了打开发动机盖积分v发v发v", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp", MessageLocation = TypeLocalMessageLocation.chatSend, });
            this.chatMessage.Add(new ChatMessageModel { Message = "5555555555555555555555555555555555555555555555555555555", MessageLocation = TypeLocalMessageLocation.chatRecv, });
            this.chatMessage.Add(new ChatMessageModel { Message = "22222222222222222222222222222222222222222222222222222", MessageLocation = TypeLocalMessageLocation.chatRecv, });
        }
    }
}
